/**
 * @project_name staff_vehicle
 * @package_name com.ssh.ser.admin
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-5-1
 * @time 上午7:58:55
 */
package com.ssh.ser.admin;

import java.util.List;

import com.ssh.dao.InnerDriverDAOI;
import com.ssh.dao.InnerVehicleDAOI;
import com.ssh.dao.StaffApplyCarDAOI;
import com.ssh.dao.StaffUseCarDAOI;
import com.ssh.entity.InnerDriver;
import com.ssh.entity.InnerVehicle;
import com.ssh.entity.StaffApplyCar;
import com.ssh.entity.StaffUseCar;
import com.ssh.model.pdata.StaffDepApplyCarMData;

/**
 * @author jack
 * @desc 派车、派遣驾驶员服务接口实现
 */
public class DispatchVDMSerImpl implements DispatchVDMSerI {
	private InnerDriverDAOI innerDriverDAOI;
	private InnerVehicleDAOI innerVehicleDAOI;
	private StaffApplyCarDAOI staffApplyCarDAOI;
	private StaffUseCarDAOI staffUseCarDAOI;

	@Override
	public String agreeDVDriver(StaffUseCar staffUseCar,
			StaffApplyCar staffApplyCar) {
		// TODO Auto-generated method stub
		try{
			StaffApplyCar sac = staffApplyCarDAOI.queryById(staffApplyCar.getStaffId(),
					staffApplyCar.getUsercarStaffid());
			sac.setApplyResult("agree");
			sac.setRComments("已批准");
			staffApplyCarDAOI.updateBySAC(sac);
			staffUseCarDAOI.addStaffUC(staffUseCar);

		}
		catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}

	/** 同意分派驾驶员 车辆 */
	@Override
	public String agreeDVDriver(String idWhoApply, String idWhoUseCar,
			String vehicleId, String driverId) {
		// TODO Auto-generated method stub
		try{
			//派遣驾驶员
			InnerDriver innerDriver = innerDriverDAOI.queryById(driverId);
			innerDriver.setState("driving");//设置驾驶员 状态
			//分配车辆
			InnerVehicle innerVehicle = innerVehicleDAOI.viewById(vehicleId);
			innerVehicle.setState("using");//更新车辆状态
			innerDriverDAOI.updateByInnerDriver(innerDriver);
			innerVehicleDAOI.update(innerVehicle);
			//更新apply申请表
			StaffApplyCar staffApplyCar = staffApplyCarDAOI.queryById(idWhoApply, idWhoUseCar);
			staffApplyCar.setApplyResult("agree");
			staffApplyCarDAOI.updateBySAC(staffApplyCar);
			//写入记录
			StaffUseCar staffUseCar = new StaffUseCar();
			staffUseCar.setDriverId(driverId);
			staffUseCar.setVehicleId(vehicleId);
			staffUseCar.setUsecarStaffid(idWhoUseCar);
			staffUseCar.setStaffId(idWhoApply);
			staffUseCar.setSucState((short)1);
			staffUseCar.setApplyDate(staffApplyCar.getApplyDate());
			staffUseCar.setState("in");
			staffUseCarDAOI.addStaffUC(staffUseCar);

		}
		catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}

	@Override
	public String declineDVDriver(String idWhoApply, String idWhoUse) {
		// TODO Auto-generated method stub
		try{
			StaffApplyCar sac = staffApplyCarDAOI.queryById(idWhoApply,idWhoUse);
			sac.setApplyResult("disagree");
			staffApplyCarDAOI.updateBySAC(sac);
		}
		catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}

	/** 不同意分派驾驶员 车辆 */
	@Override
	public String declineDVDriver(String idWhoApply, String idWhoUseCar,
			String vehicleId, String driverId) {
		// TODO Auto-generated method stub
		try{
			//更新apply申请表
			StaffApplyCar staffApplyCar = staffApplyCarDAOI.queryById(idWhoApply, idWhoUseCar);
			staffApplyCar.setApplyResult("decline");
			staffApplyCarDAOI.updateBySAC(staffApplyCar);
		}
		catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}
	
	/** 不同意分派驾驶员 车辆 */
	@Override
	public String declineDVDriver(int idstaffApplyCar) {
		// TODO Auto-generated method stub
		try{
			//更新apply申请表
			StaffApplyCar staffApplyCar = staffApplyCarDAOI.queryById(idstaffApplyCar);
			staffApplyCar.setApplyResult("decline");
			staffApplyCarDAOI.updateBySAC(staffApplyCar);
		}
		catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}

	/**
	 * @return the innerDriverDAOI
	 */
	public InnerDriverDAOI getInnerDriverDAOI() {
		return innerDriverDAOI;
	}

	/**
	 * @return the innerVehicleDAOI
	 */
	public InnerVehicleDAOI getInnerVehicleDAOI() {
		return innerVehicleDAOI;
	}

	/**
	 * @return the staffApplyCarDAOI
	 */
	public StaffApplyCarDAOI getStaffApplyCarDAOI() {
		return staffApplyCarDAOI;
	}

	/**
	 * @return the staffUseCarDAOI
	 */
	public StaffUseCarDAOI getStaffUseCarDAOI() {
		return staffUseCarDAOI;
	}

	/**
	 * @param innerDriverDAOI
	 *            the innerDriverDAOI to set
	 */
	public void setInnerDriverDAOI(InnerDriverDAOI innerDriverDAOI) {
		this.innerDriverDAOI = innerDriverDAOI;
	}

	/**
	 * @param innerVehicleDAOI
	 *            the innerVehicleDAOI to set
	 */
	public void setInnerVehicleDAOI(InnerVehicleDAOI innerVehicleDAOI) {
		this.innerVehicleDAOI = innerVehicleDAOI;
	}

	/**
	 * @param staffApplyCarDAOI
	 *            the staffApplyCarDAOI to set
	 */
	public void setStaffApplyCarDAOI(StaffApplyCarDAOI staffApplyCarDAOI) {
		this.staffApplyCarDAOI = staffApplyCarDAOI;
	}

	/**
	 * @param staffUseCarDAOI
	 *            the staffUseCarDAOI to set
	 */
	public void setStaffUseCarDAOI(StaffUseCarDAOI staffUseCarDAOI) {
		this.staffUseCarDAOI = staffUseCarDAOI;
	}

	@Override
	public List<InnerDriver> viewCanDDriver(String state) {
		// TODO Auto-generated method stub
		return innerDriverDAOI.queryByState(state);
	}

	@Override
	public List<InnerVehicle> viewCanDVehicleByLevel(String vehicleLevel) {
		// TODO Auto-generated method stub
		return innerVehicleDAOI.queryNUseByLevel(vehicleLevel);
	}

	@Override
	public InnerDriver viewDriverByDId(String driverId) {
		// TODO Auto-generated method stub
		return innerDriverDAOI.queryById(driverId);
	}

	/** 查看有哪些员工需要申请*/
	@Override
	public List<StaffDepApplyCarMData> viewWhoWToApply() {
		// TODO Auto-generated method stub
		return staffApplyCarDAOI.getAllApplyCar();
	}

	/** 查看某位员工申请*/
	@Override
	public List<StaffDepApplyCarMData> viewOneWaitingApply(String idWhoApply, String idWhoUse,String applyResult) {
		// TODO Auto-generated method stub
		return staffApplyCarDAOI.getOneWaitingApplyCar(idWhoApply, idWhoUse, applyResult);
	}

}
